<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: Drag Delegation with a Drop Target</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: Drag Delegation with a Drop Target</h1>

    
        <a href="#toc" class="jump">Jump to Table of Contents</a>
    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><div class="intro">
<p>This example shows how to create multiple draggable items efficiently, still allowing for Drop Targets.</p>
</div>

<div class="example">
    <style>
    #demo {
        width: 200px;
    }
    #demo ul li {
        border: 1px solid black;
        background-color: #8DD5E7;
        cursor: move;
        margin: 3px;
        list-style-type: none;
        z-index: 2;
        zoom: 1;
    }
    #play {
        position: relative;
        zoom: 1;
    }
    #drop {
        border: 1px solid black;
        background-color: #eee;
        height: 50px;
        width: 200px;
        position: absolute;
        bottom: 5px;
        right: 5px;
        zoom: 1;
    }
    #drop strong {
        font-weight: bold;
    }
    #drop.yui3-dd-drop-over {
        background-color: #ccc;
    }


</style>

    <div id="play">

        <div id="demo">
            <ul>
                <li>Item #1</li>
                <li>Item #2</li>
                <li>Item #3</li>
                <li>Item #4</li>
                <li>Item #5</li>
                <li>Item #6</li>
                <li>Item #7</li>
                <li>Item #8</li>
                <li>Item #9</li>
                <li>Item #10</li>
            </ul>
        </div>
        
        <div id="drop">Drop on me</div>
    </div>



<script>
    YUI().use('dd-delegate', 'dd-drop-plugin', function(Y) {
        var del = new Y.DD.Delegate({
            container: '#demo',
            nodes: 'li'
        });

        del.on('drag:end', function(e) {
            del.get('currentNode').setStyles({
                top: 0,
                left: 0
            });
        });

        var drop = Y.one('#drop').plug(Y.Plugin.Drop);
        drop.drop.on('drop:hit', function(e) {
            drop.set('innerHTML', 'You dropped: <strong>' + e.drag.get('node').get('innerHTML') + '</strong>');
        });
        
    });


</script>


</div>

<h3 id="setting-up-the-delegate-container-and-items">Setting up the Delegate container and items</h3>
<p>First we need to create an HTML Node to act as the delegate container and give it some nodes to make draggable.</p>

<pre class="code prettyprint">&lt;div id=&quot;play&quot;&gt;

    &lt;div id=&quot;demo&quot;&gt;
        &lt;ul&gt;
            &lt;li&gt;Item #1&lt;&#x2F;li&gt;
            &lt;li&gt;Item #2&lt;&#x2F;li&gt;
            &lt;li&gt;Item #3&lt;&#x2F;li&gt;
            &lt;li&gt;Item #4&lt;&#x2F;li&gt;
            &lt;li&gt;Item #5&lt;&#x2F;li&gt;
            &lt;li&gt;Item #6&lt;&#x2F;li&gt;
            &lt;li&gt;Item #7&lt;&#x2F;li&gt;
            &lt;li&gt;Item #8&lt;&#x2F;li&gt;
            &lt;li&gt;Item #9&lt;&#x2F;li&gt;
            &lt;li&gt;Item #10&lt;&#x2F;li&gt;
        &lt;&#x2F;ul&gt;
    &lt;&#x2F;div&gt;
    
    &lt;div id=&quot;drop&quot;&gt;Drop on me&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;</pre>


<p>Now we give them some CSS to make them visible.</p>

<pre class="code prettyprint">#demo {
    width: 200px;
}
#demo ul li {
    border: 1px solid black;
    background-color: #8DD5E7;
    cursor: move;
    margin: 3px;
    list-style-type: none;
    z-index: 2;
    zoom: 1;
}
#play {
    position: relative;
    zoom: 1;
}
#drop {
    border: 1px solid black;
    background-color: #eee;
    height: 50px;
    width: 200px;
    position: absolute;
    bottom: 5px;
    right: 5px;
    zoom: 1;
}
#drop strong {
    font-weight: bold;
}
#drop.yui3-dd-drop-over {
    background-color: #ccc;
}</pre>


<h3 id="setting-up-the-yui-instance">Setting up the YUI Instance</h3>
<p>Now we need to create our YUI instance and tell it to load the <code>dd-delegate</code> module.</p>

<pre class="code prettyprint">YUI().use(&#x27;dd-delegate&#x27;, &#x27;dd-drop-plugin&#x27;);</pre>


<h3 id="making-the-nodes-draggable">Making the Nodes draggable</h3>
<p>Now that we have a YUI instance with the <code>dd-delegate</code> module, we need to instantiate the <code>Delegate</code> instance on this container and nodes.</p>

<pre class="code prettyprint">YUI().use(&#x27;dd-delegate&#x27;, &#x27;dd-drop-plugin&#x27;, function(Y) {
    var del = new Y.DD.Delegate({
        container: &#x27;#demo&#x27;,
        nodes: &#x27;li&#x27;
    });
});</pre>


<h3 id="listening-to-some-events">Listening to some events</h3>
<p>The <code>Delegate</code> object is a bubble target for the <code>DD.Drag</code> instances. So you can listen on it for all drag related events.</p>

<pre class="code prettyprint">YUI().use(&#x27;dd-delegate&#x27;, &#x27;dd-drop-plugin&#x27;, function(Y) {
    var del = new Y.DD.Delegate({
        container: &#x27;#demo&#x27;,
        nodes: &#x27;li&#x27;
    });

    del.on(&#x27;drag:end&#x27;, function(e) {
        del.get(&#x27;currentNode&#x27;).setStyles({
            top: 0,
            left: 0
        });
    });

});</pre>


<h3 id="adding-a-drop-target">Adding a Drop Target</h3>
<p>Now we can add a normal Drop Target to the page.</p>

<pre class="code prettyprint">var drop = Y.one(&#x27;#drop&#x27;).plug(Y.Plugin.Drop);</pre>


<p>Once that is created, we can add a <code>drop:hit</code> listener and manipulate the drag instance as we normally would.</p>

<pre class="code prettyprint">var drop = Y.one(&#x27;#drop&#x27;).plug(Y.Plugin.Drop);
drop.drop.on(&#x27;drop:hit&#x27;, function(e) {
    drop.set(&#x27;innerHTML&#x27;, &#x27;You dropped: &lt;strong&gt;&#x27; + e.drag.get(&#x27;node&#x27;).get(&#x27;innerHTML&#x27;) + &#x27;&lt;&#x2F;strong&gt;&#x27;);
});</pre>


<h3 id="full-javascript-source">Full Javascript Source</h3>

<pre class="code prettyprint">YUI().use(&#x27;dd-delegate&#x27;, &#x27;dd-drop-plugin&#x27;, function(Y) {
    var del = new Y.DD.Delegate({
        container: &#x27;#demo&#x27;,
        nodes: &#x27;li&#x27;
    });

    del.on(&#x27;drag:end&#x27;, function(e) {
        del.get(&#x27;currentNode&#x27;).setStyles({
            top: 0,
            left: 0
        });
    });

    var drop = Y.one(&#x27;#drop&#x27;).plug(Y.Plugin.Drop);
    drop.drop.on(&#x27;drop:hit&#x27;, function(e) {
        drop.set(&#x27;innerHTML&#x27;, &#x27;You dropped: &lt;strong&gt;&#x27; + e.drag.get(&#x27;node&#x27;).get(&#x27;innerHTML&#x27;) + &#x27;&lt;&#x2F;strong&gt;&#x27;);
    });
    
});</pre>

</div>
        </div>

        <div id="sidebar" class="yui3-u">
            
                <div id="toc" class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Table of Contents</h2>
                    </div>

                    <div class="bd">
                        <ul class="toc">
<li>
<a href="#setting-up-the-delegate-container-and-items">Setting up the Delegate container and items</a>
</li>
<li>
<a href="#setting-up-the-yui-instance">Setting up the YUI Instance</a>
</li>
<li>
<a href="#making-the-nodes-draggable">Making the Nodes draggable</a>
</li>
<li>
<a href="#listening-to-some-events">Listening to some events</a>
</li>
<li>
<a href="#adding-a-drop-target">Adding a Drop Target</a>
</li>
<li>
<a href="#full-javascript-source">Full Javascript Source</a>
</li>
</ul>
                    </div>
                </div>
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="A simple drag interaction that doesn&#x27;t require a drop interaction.">
                                        <a href="simple-drag.html">Simple Drag</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to apply the Drag Plugin to a node.">
                                        <a href="drag-plugin.html">Drag - Node plugin</a>
                                    </li>
                                
                            
                                
                                    <li data-description="A simple proxy drag interaction that doesn&#x27;t require a drop interaction.">
                                        <a href="proxy-drag.html">Drag - Proxy</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to constrain a draggable Node to another Node&#x27;s region.">
                                        <a href="constrained-drag.html">Drag - Constrained to a Region</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Using interaction groups, this example demonstrates how to tie into the Drag &amp; Drop Utility&#x27;s interesting moments to provide visual affordances for the current drag operation.">
                                        <a href="groups-drag.html">Drag - Interaction Groups</a>
                                    </li>
                                
                            
                                
                                    <li data-description="The use of the drag shim when dragging nodes over other troublesome nodes.">
                                        <a href="shim-drag.html">Using the Drag Shim</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to use the Drop Target events to code your application.">
                                        <a href="drop-code.html">Using Drop Based Coding</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How you can use the DD Scroll plugin to scroll the browser window as you drag.">
                                        <a href="winscroll.html">Window Scrolling</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to use DD.Delegate to create a scalable solution which supports multiple draggable items.">
                                        <a href="delegate.html">Drag Delegation</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Using DD.Delegate to support dragging multiple items and dropping them onto a Drop Target.">
                                        <a href="delegate-drop.html">Drag Delegation with a Drop Target</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to use Drag plugins with a DD Delegate based solution.">
                                        <a href="delegate-plugins.html">Using Drag Plugins with Delegate</a>
                                    </li>
                                
                            
                                
                                    <li data-description="This example shows how to make a sortable list using Custom Event Bubbling.">
                                        <a href="list-drag.html">List Reorder w/Bubbling</a>
                                    </li>
                                
                            
                                
                                    <li data-description="This example shows how to make a sortable list using Custom Event Bubbling and Node Scrolling.">
                                        <a href="scroll-list.html">List Reorder w/Scrolling</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to make an animated node a Drop target.">
                                        <a href="anim-drop.html">Animated Drop Targets</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Example Photo Browser application.">
                                        <a href="photo-browser.html">Photo Browser</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Portal style example using Drag &amp; Drop Event Bubbling and Animation.">
                                        <a href="portal-drag.html">Portal Style Example</a>
                                    </li>
                                
                            
                                
                            
                                
                            
                        </ul>
                    </div>
                </div>
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples That Use This Component</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                                    <li data-description="Working with multiple YUI instances.">
                                        <a href="../yui/yui-multi.html">Multiple Instances</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input">
                                        <a href="../stylesheet/stylesheet-theme.html">Adjusting a Page Theme on the Fly</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
